# -*- coding:utf-8 -*- 

"""
    @version: 1.0
    @author: Danson
    @software: PyCharm
    @file: tn_hotel_by_url_m_spider.py
    @time: 2017/8/3 17:24
    @description:根据途牛 WAP站的url获取酒店相关数据(通过页面获取酒店基本信息，接口获取房型数据)
    @todo:
"""


import random
import requests
import scrapy
import signal
from scrapy.selector import Selector

from scrapy_splash import SplashRequest
import traceback
from scrapy.http import Request
from scrapy import signals
import os, sys
from pangolin.common.date_common import DateCommon
from pangolin.common.string_common import StringCommon
from pangolin.middlewares import RotateUserAgentMiddleware, ProxyMiddleware
from pangolin.spider_enum.companyCodeEnum import getCompanyCode
from pangolin.common.db_operateCommon import *
from pangolin.items import HotelDetailItem
from pangolin.common.http_common import *
import logging
import re
from pangolin.settings import IS_SAVE_SERVER
from pangolin.settings import IS_SNAPSHOT
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from PIL import Image
from datetime import *
import time as times

reload(sys)
sys.setdefaultencoding('UTF-8')


class ElHotelSpider(scrapy.Spider):
    name = 'tn_hotel_by_url_m_spider'
    allowed_domain = 'tuniu.com'

    def __init__(self, crawler, spider_name, city_code, company_code, task_id, task_date, timer_param,
                 timer_granularity, timer_id, url, lv_product_id, miss_product_flag):
        # 接受入参,全局引用
        self.save_server = IS_SAVE_SERVER
        self.snapshot = IS_SNAPSHOT

        self.spider_name = spider_name
        self.city_code = city_code
        self.company_code = company_code
        self.task_id = task_id
        self.task_date = task_date
        self.timer_param = timer_param
        self.timer_granularity = timer_granularity
        self.timer_id = timer_id
        self.url = url
        self.lv_product_id = lv_product_id
        self.miss_product_flag = miss_product_flag
        # self.splash_request = True

        db_operate = DbOperateClass()
        urls = []
        lv_product_ids = {}
        mode = re.compile(r'\d+')
        self.detail_url = url
        # self.detail_url ="http://m.tuniu.com/api/hotel/product/hotelStaticInfo?d=%7B%22productId%22%3A"+mode.findall(self.url)[0]+"%7D"
        if self.miss_product_flag == 'FALSE':
            sql = "SELECT tn_url,product_id FROM cpa_care_product WHERE city_code = '" + self.city_code + \
                  "' AND business_code='HOTEL'"
            try:
                (fc, count) = db_operate.select_by_sql(sql)
                for row in fc:
                    if row[0] is not None and row[0] != "":
                        url = row[0]
                        product_id = mode.findall(url)[0]
                        lv_product_id = row[1]
                        new_url = "http://m.tuniu.com/api/hotel/product/hotelStaticInfo?d=%7B%22productId%22%3A" + product_id + "%7D"
                        lv_product_ids.setdefault(new_url, lv_product_id)
                        urls.append(new_url)
                self.lv_product_ids = lv_product_ids
            except Exception as e:
                logging.info("Exception %s" % e)
                raise
            # 爬虫爬取前先删除同一批次同一城市的数据
            db_operate = DbOperateClass()
            db_operate.delete_pd_and_cd_data("hotel_pdetail", "hotel_cdetail", getCompanyCode(company_code),
                                             city_code, task_id, task_date)
        else:
            product_id = mode.findall(self.url)[0]
            self.url = "http://m.tuniu.com/api/hotel/product/hotelStaticInfo?d=%7B%22productId%22%3A" + product_id + "%7D"
            lv_product_ids.setdefault(self.url, self.lv_product_id)
            self.lv_product_ids = lv_product_ids
            urls.append(self.url)
            self.lv_product_ids = lv_product_ids
        self.start_urls = urls
        self.base_urls = "http://m.tuniu.com"
        # 绑定爬虫开始和结束信号，便于监听爬虫开始与结束
        self.crawler = crawler
        cs = crawler.signals
        cs.connect(self.spider_closed, signals.spider_closed)
        cs.connect(self.spider_opened, signals.spider_opened)

    def parse(self, response):
        # print response.body
        sel = json.loads(response.body)
        item = HotelDetailItem()
        try:
            item['type'] = "HOTEL"
            item['timer_param'] = self.timer_param
            item['timer_granularity'] = self.timer_granularity
            item['business_type'] = "hotel"

            item['product_table'] = "hotel_pdetail"
            item['data_type'] = "HotelDetail"
            item['detail_id'] = DateCommon.get_id_by_datetime()
            item['company_code'] = getCompanyCode(self.company_code)
            item['platform_code'] = "WAP"  # 操作平台WAP，PC
            item['city_code'] = self.city_code
            dr = re.compile(r'<[^>]+>', re.S)
            try:
                item['hotel_name'] = StringCommon.remove_blank(sel['data']['name'])
            except Exception as e:
                item['hotel_name']=StringCommon.remove_blank(sel['data'][0]['name'])
            mode = re.compile(r'\d+')
            item['product_id'] = sel['data']['productId']
            item['come_date'] = DateCommon.get_current_date()
            item['leave_date'] = DateCommon.get_other_date(1)
            item['hotel_addr'] = sel['data']['hotelLocation']
            # item['lowest_price'] = room_data['value']['lowestProduct']['avgSalePrice']
            item['score'] = sel['data']['totalScore']
            item['image_num'] = ""
            item['introduction'] = ""
            item['score_tag'] = ""
            item['star_level'] = sel['data']['hotelLevel']
            item['detail_url'] = "http://m.tuniu.com/hotel/detail/{}?checkInDate={}&checkOutDate={}&roomType=0"\
                .format(item['product_id'], item['come_date'], item['leave_date'])
            if self.snapshot == 'TRUE':
                item['snapshot_addr'] = " "
                # 获得截图地址
                # compress_path = self.save_screenshot(response, item['product_id'], self.city_code,
                #                                      self.company_code, self.task_id, self.task_date)
                # item['snapshot_addr'] = compress_path
            else:
                item['snapshot_addr'] = " "
            item['lv_product_id'] = self.lv_product_ids.get(response.url)
            item['create_time'] = DateCommon.get_current_date()
            item['update_time'] = item['create_time']
            item['task_id'] = self.task_id
            item['task_date'] = self.task_date

            # 商品详情
            item['commodity_table'] = "hotel_cdetail"
            item['room_type'] = []  # 房型
            item['commodity_id'] = []  # 商品id
            item['area'] = []  # 房间面积
            item['floor'] = []  # 楼层
            item['commodity_name'] = []  # 商品名称
            item['bed_type'] = []  # 床型
            item['broadband'] = []  # 宽带
            item['breakfast'] = []  # 早餐
            item['window'] = []  # 窗户
            item['increase_bed'] = []  # 加床
            item['people_num'] = []  # 入住人数
            item['commodity_url'] = []  # 商品预定链接
            item['cancel_rule'] = []  # 取消(退订)规则
            item['preferential'] = []  # 优惠
            item['sale_price'] = []  # 售价

            room_type_url = "http://m.tuniu.com/api/hotel/product/hotelRoomRatePlanM?d=%7B%22productId%22%3A{}%2C%22" \
                            "checkInDate%22%3A%22{}%22%2C%22checkOutDate%22%3A%22{}%22%7D"\
                .format(item['product_id'], item['come_date'], item['leave_date'])
            self.count = 0
            request = Request(room_type_url, meta={'item': item}, callback=self.parse_commodity,
                              errback=self.handel_erro)
            yield request

        except Exception as e:
            self.exception_handel(response, e)

    def parse_commodity(self, response):
        item = response.meta['item']
        room_data = json.loads(response.body)
        #当当天的房型没有时换个日期进行抓取
        # if len(room_data['data'])<1:
        #     if self.count < 3:
        #         self.count += 1
        #         print self.count
        #         item['come_date']=DateCommon.get_other_date(self.count)
        #         item['leave_date']=DateCommon.get_other_date(self.count+1)
        #         print item['come_date'],item['leave_date']
        #         url = "http://m.tuniu.com/api/hotel/product/hotelRoomRatePlanM?d=%7B%22productId%22%3A{}%2C%22" \
        #                     "checkInDate%22%3A%22{}%22%2C%22checkOutDate%22%3A%22{}%22%7D"\
        #         .format(item['product_id'], item['come_date'], item['leave_date'])
        #         print url
        #         yield Request(url, dont_filter=True)
        # else:
        if len(room_data['data'])>0:
            package=room_data['data'][0]['packages']
            if len(package)>0:
                item['lowest_price'] = room_data['data'][0]['packages'][0]['cashBackdPrice']
            else:
                item['lowest_price'] =""
        else:
            item['lowest_price'] = ""
        sites_evens = room_data['data']
        # check_in = room_data['checkinDate']
        # check_out = room_data['checkoutDate']
        for site in sites_evens:
            room_type = site['name']
            increase_bed = site['hotelHouseServiceInfo']['houseServiceInfoArray'][2]['serviceName']
            sites_even = site['packages']
            for channel_site in sites_even:
                commodity_name = channel_site['name']
                area = channel_site['area']
                floor = channel_site['floor']
                has_window = channel_site['hasWindow']
                if has_window == 2:
                    window = "有窗"
                elif has_window == 1:
                    window = "部分有窗"
                else:
                    window=""

                people_num = channel_site['adultNum']
                bed_type = channel_site['bedType']
                broadband = channel_site['netName']
                breakfast = channel_site['breakfast']
                cancel_rule = channel_site['cancelText'] + "。" + channel_site['cancellationPolicy']
                commodity_id = channel_site['roomId']
                preferential = channel_site['cashBack']
                sale_price = channel_site['cashBackdPrice']
                commodity_url = ""

                item['room_type'].append(room_type)  # 商品类型或名称
                item['commodity_id'].append(commodity_id)  # 商品id
                item['area'].append(area)  # 房间面积
                item['floor'].append(floor)  # 楼层
                item['commodity_name'].append(commodity_name)  # 商品名称
                item['bed_type'].append(bed_type)  # 床型
                item['broadband'].append(broadband)  # 宽带
                item['breakfast'].append(breakfast)  # 早餐
                item['window'].append(window)  # 窗户
                item['increase_bed'].append(increase_bed)  # 加床
                item['people_num'].append(people_num)  # 入住人数
                item['commodity_url'].append(commodity_url)  # 商品预定链接
                item['cancel_rule'].append(cancel_rule)  # 取消(退订)规则
                item['preferential'].append(preferential)  # 优惠
                item['sale_price'].append(sale_price)  # 售价
        yield item

    # 按元素截图
    def save_screenshot(self, response, product_id, city_code, company_code, task_id, task_date):
        compress_path = ""
        try:
            file_name = getCompanyCode(
                company_code) + "__" + city_code + "__" + task_id + "__" + task_date + "__" + product_id + ".png"
            service_args = [
                '--proxy=' + response.meta['proxy'],
                # '--load-images=no',
                # '--ignore-ssl-errors=true',
                '--ssl-protocol=any'
            ]
            dcap = dict(DesiredCapabilities.PHANTOMJS)
            # # 伪造ua信息
            middleware = RotateUserAgentMiddleware()
            user_agent = random.choice(middleware.user_agent_list)
            dcap["phantomjs.page.settings.userAgent"] = user_agent
            dcap["phantomjs.page.settings.resourceTimeout"] = 5000  # 超时
            dcap["browserName"] = "Netscape"
            dcap["version"] = "5.0"
            # 添加头文件
            dcap["phantomjs.page.customHeaders.Referer"] = response.url
            driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=service_args)  # 线上使用方式
            logging.info("%s PhantomJS is getUrl..." % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            driver.set_page_load_timeout(100)
            driver.set_script_timeout(100)
            driver.get(response.url)
            times.sleep(2)
            # print driver.page_source
            # 截图
            path = os.getcwd() + "/screenshot/" + file_name
            compress_path = os.getcwd() + "/compressImage/" + file_name
            logging.info("%s save_screenshot is starting..." % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            driver.save_screenshot(path)
            logging.info("%s save_screenshot is end..." % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            # 根据元素获得裁剪区域的坐标
            element = driver.find_element_by_css_selector('.shadow')
            element1 = driver.find_element_by_xpath("//*[@class='content-wrap sceneryBook']")
            left = int(element.location['x'])
            top = int(element.location['y'])
            right = int(element.location['x'] + element.size['width'])
            bottom = int(element.location['y'] + element.size['height'] + element1.size['height'])
            # 根据获得的裁剪区域坐标截图
            compress_path = os.getcwd() + "/compressImage/" + file_name
            im = Image.open(path)
            im = im.crop((left, top, right, bottom))  # (left, top, right, bottom)
            im.save(compress_path)
            os.remove(path)
        except Exception as e:
            logging.error("截图失败 %s ,%s" % (response.url, e))
            logging.error("截图 页面源码：%s ,%s" % (response.url, driver.page_source))
        finally:
            try:
                driver.service.process.send_signal(signal.SIGTERM)
                driver.quit()
            except Exception as e:
                logging.error("driver.quit_failed %s" % e)
        return compress_path

    def handel_erro(self):
        logging.error("Inside_handel_erro")

    def exception_handel(self, response, e):
        logging.error("记录日志：标签变动 %s ,%s" % (response.url, e))
        logging.info("页面源码：%s ,%s" % (response.url, response.body))
        logging.error("Exception: %s" % traceback.format_exc().replace("\"", "'"))
        db_operate = DbOperateClass()
        # 错误描述只截取前500个字符，剩下的用***代替
        error_desc = traceback.format_exc().replace("\"", "'").replace(" ", "")
        if len(error_desc) > 500:
            error_desc = error_desc[:500] + '***'
        # 错误日志记入日志表
        sql = "insert into cpa_oper_log (log_type,company_code,busi_type,url,batch_num,timer_code,error_code,error_desc,task_id,task_date) values (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\" ,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")" \
              % ("C", getCompanyCode(self.company_code), "酒店", response.url, self.task_date, "timer_code",
                 "error_code", error_desc, str(self.task_id), str(self.task_date))
        db_operate.update_by_sql(sql)
        if self.save_server == 'TRUE':
            http_common = HttpCommon()
            operate_log = {'type': 'LOG',
                           'log_type': "C",
                           'company_code': getCompanyCode(self.company_code),
                           'busi_type': "酒店",
                           'url': response.url,
                           'batch_num': self.task_date,
                           'timer_code': "timer_code",
                           'error_code': "error_code",
                           'error_desc': error_desc,
                           'task_id': str(self.task_id),
                           'task_date': str(self.task_date)}
            http_common.get_method(operate_log)

    @classmethod
    def from_crawler(cls, crawler, **kwargs):
        logging.info('inside_from_crawler: ')
        if kwargs['miss_product_flag'] == 'FALSE':
            kwargs['url'] = ''
            kwargs['lv_product_id'] = ''
        else:
            kwargs['timer_granularity'] = ''
            kwargs['timer_id'] = ''

        return cls(
            crawler,
            spider_name=kwargs['spider_name'],
            city_code=kwargs['city_code'],
            company_code=kwargs['company_code'],
            task_id=kwargs['task_id'],
            task_date=kwargs['task_date'],
            timer_param=kwargs['timer_param'],
            timer_granularity=kwargs['timer_granularity'],
            timer_id=kwargs['timer_id'],
            url=kwargs['url'],
            lv_product_id=kwargs['lv_product_id'],
            miss_product_flag=kwargs['miss_product_flag']
        )

    def spider_opened(self, spider):
        logging.info('Spider_opened_list: %s' % spider.name)
        # 爬虫设为启动状态2
        if self.miss_product_flag == 'FALSE':
            db_operate = DbOperateClass()
            db_operate.update_status(2, "PYTHON", self.timer_id)

    def spider_closed(self, spider, reason):
        logging.info('spider_closed_list: %s ,reason:%s' % (spider.name, reason))
        db_operate = DbOperateClass()
        if self.miss_product_flag == 'TRUE':  # 补录
            db_operate.update_miss_status(1, self.detail_url, self.task_id)
        elif self.miss_product_flag == 'FALSE':
            sql = "SELECT timer.timer_status FROM cpa_timer timer WHERE timer.timer_type='PYTHON' AND timer.timer_id " \
                  "= '" + self.timer_id + "'"
            (fc, count) = db_operate.select_by_sql(sql)
            spider.logger.info('SELECT_COUNT: %s ,SQL:%s' % (count, sql))
            timer_status = ""
            for row in fc:
                timer_status = row[0]
            logging.info("timer_status:%s" % timer_status)
            if timer_status == '2':
                db_operate.update_status(5, "PYTHON", self.timer_id)